package com.zsf.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zsf.pojo.User;
import com.zsf.service.UserService;
import com.zsf.serviceImp.UserServiceImp;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	UserService userService=new UserServiceImp();
	

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		User user =new User();
		String account=request.getParameter("account");
		String password=request.getParameter("password");
		PrintWriter pw=response.getWriter();
		System.out.println(password+"         "+account);
		user.setAccount(account);
		user.setPassword(password);
		user=userService.getUserByAP(user);
		if(user!=null){
			request.getSession().setAttribute("current_user", user);
			pw.print("<script>alert('登录成功，点击确定跳转到商场主页'); window.location.href='productIndex';</script>");
		}else{
			pw.print("<script>alert('登录失败，请重新登录'); window.location.href='login.jsp';</script>");
		}
	}

}
